package clients;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

import shared.Status;
import shared.Stock;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class StockPriceDialog extends JDialog {
	private Stock stock;
	private Client client;
	private final JPanel contentPanel = new JPanel();
	private JTextField textFieldStockName;
	private JTextField textFieldCurrentPrice;
	private JTextField textFieldNewPrice;


	/**
	 * Create the dialog.
	 */
	public StockPriceDialog(Stock newStock, Client newClient) 
	{
		this.stock = newStock;
		this.client = newClient;
		

		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblStockName = new JLabel("Stock Name:");
			lblStockName.setBounds(55, 34, 88, 19);
			contentPanel.add(lblStockName);
		}
		
		textFieldStockName = new JTextField();
		textFieldStockName.setEditable(false);
		textFieldStockName.setBounds(169, 33, 142, 20);
		contentPanel.add(textFieldStockName);
		textFieldStockName.setColumns(10);
		
		JLabel lblCurrentPrice = new JLabel("Current Price:");
		lblCurrentPrice.setBounds(55, 93, 88, 14);
		contentPanel.add(lblCurrentPrice);
		
		textFieldCurrentPrice = new JTextField();
		textFieldCurrentPrice.setEditable(false);
		textFieldCurrentPrice.setBounds(169, 90, 142, 20);
		contentPanel.add(textFieldCurrentPrice);
		textFieldCurrentPrice.setColumns(10);
		
		JLabel lblNewPrice = new JLabel("New Price:");
		lblNewPrice.setBounds(55, 152, 74, 14);
		contentPanel.add(lblNewPrice);
		
		textFieldNewPrice = new JTextField();
		textFieldNewPrice.setBounds(170, 149, 141, 20);
		contentPanel.add(textFieldNewPrice);
		textFieldNewPrice.setColumns(10);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) 
					{

							double newPrice = 0.0;
							try
							{
								newPrice = Double.parseDouble(textFieldNewPrice.getText());
							}
							catch (Exception ee)
							{
								JOptionPane.showMessageDialog(null, "Input error, please try again!");
								textFieldNewPrice.setText("");
								return;
							}
							if (newPrice <= 0)
							{
								JOptionPane.showMessageDialog(null, "Input error, please try again!");
								textFieldNewPrice.setText("");
								return;
							}
							
							
							Status status = client.modify(stock.getName(), newPrice);
							if (status.getCode() == 0)
							{
								JOptionPane.showMessageDialog(null, "Price change succeed!");
								dispose();
							}
							else
							{
								JOptionPane.showMessageDialog(null, status.getMessage());
								dispose();
							}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		
		this.textFieldStockName.setText(stock.getName());
		String priceString = String.format("%.2f", stock.getPrice());
		this.textFieldCurrentPrice.setText(priceString);
	}
}
